;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var  rightsList = {
        roleColumnsInit:function(){
            return  [
	            {
	            	checkbox: true
	            },
	            {
	            	title: '角色名称',
	                field: 'roleName',
	                width: '100px',
	                sortable : true
	            },
	            {
	            	title: '角色编码',
	                field: 'roleCode',
	                sortable : true
	            }
	        ];
        },
        selectRoleColumnsInit:function(){
            return  [
	            {
	            	checkbox: true
	            },
	            {
	            	title: '角色名称',
	                field: 'roleName',
	                width: '100px',
	                sortable : true
	            },
	            {
	            	title: '角色编码',
	                field: 'roleCode',
	                sortable : true
	            }
	        ];
        },
        roleGridInit:function(columns){
            return $("#roleTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                clickToSelect: true,
                classes: 'table table-hover qxttable table-com roletable',
                height: 305,
                onCheck: function(){
                	changeArrowStatus("roleTable");
                },
                onUncheck: function(){
                	changeArrowStatus("roleTable");
                },
                onCheckAll: function(){
                	changeArrowStatus("roleTable");
                },
                onUncheckAll: function(){
                	changeArrowStatus("roleTable");
                }
            })

        },
        selectRoleGridInit:function(columns){
            return $("#selectRoleTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                clickToSelect: true,
                classes: 'table table-hover qxttable  table2 table-com roletable',
                height: 305,
                onCheck: function(){
                	changeArrowStatus("selectRoleTable");
                },
                onUncheck: function(){
                	changeArrowStatus("selectRoleTable");
                },
                onCheckAll: function(){
                	changeArrowStatus("selectRoleTable");
                },
                onUncheckAll: function(){
                	changeArrowStatus("selectRoleTable");
                }
            })

        },
        roleDataLoad:function(){
        	$("#roleTable").bootstrapTable("removeAll");
        	$("#roleTable").bootstrapTable("showLoading");
        	var userCode = $('#selectUserCode').val();
        	var queryRole = $('#queryRole').val();
        	var args = {};
        	args.userCode = userCode;
        	args.roleName = queryRole;
    		args.roleCode = queryRole;
            $$.ajax({
		        url:"/rest/BaseRoleBusiness/listByCodeOrName",
		        data : args,
		        success : function(data){
		            $("#roleTable").bootstrapTable("load",data);
		            changeArrowStatus("roleTable");
		            $("#roleTable").bootstrapTable("hideLoading");
		        }
		    });
        },
        roleSelectDataLoad:function(){
        	$("#selectRoleTable").bootstrapTable("removeAll");
        	$("#selectRoleTable").bootstrapTable("showLoading");
        	var userCode = $('#selectUserCode').val();
        	var querySelectRole = $('#querySelectRole').val();
        	var args = {};
        	args.userCode = userCode;
    		args.roleCode = querySelectRole;
        	args.roleName = querySelectRole;
        	$$.ajax({
		        url:"/rest/BaseUserRoleBusiness/listRoleByUserCode",
		        data : args,
		        success : function(data){
		            $("#selectRoleTable").bootstrapTable("load",data);
		            changeArrowStatus("selectRoleTable");
		            $("#selectRoleTable").bootstrapTable("hideLoading");
		        }
		    });
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = rightsList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return rightsList;});
    } else {
        !('plugin' in _global) && (_global.rightsList = rightsList);
    }
}());

/**
 * 初始化数据
 * @param {Object} roleCode
 */
function initData(userCode){
	$('#selectUserCode').val(userCode);
	rightsList.roleDataLoad();
	rightsList.roleSelectDataLoad();
}

//加载数据后移动数据箭头按钮颜色控制
function changeArrowStatus(tableId){
	//获取选中行
	var selectRowDatas = $("#"+tableId).bootstrapTable('getSelections');
	if(selectRowDatas && selectRowDatas.length>0){
		if("roleTable"==tableId){
			$('#addToRight').addClass('click-arrow-on');
		}else if("selectRoleTable"==tableId){
			$('#removeRight').addClass('click-arrow-on');
		}
	}else{
		if("roleTable"==tableId){
			$('#addToRight').removeClass('click-arrow-on');
		}else if("selectRoleTable"==tableId){
			$('#removeRight').removeClass('click-arrow-on');
		}
	}
}

/**
 * 保存数据
 * @param {Object} userCodes
 */
function saveRoleCodes(roleCodes){
	var result = false;
	var userCode = $('#selectUserCode').val();
	var args = {};
	args.userCode = userCode;
	args.roleCodes = roleCodes.join(',');
	$$.ajax({
        url:"/rest/BaseUserRoleBusiness/saveUserAndRoleData",
        async:false,
        data : args,
        success : function(data){
        	result = true;
        },
        error: function(){
        	result = false;
        }
    });
	return result;
}

/**
 * 删除数据
 * @param {Object} userCodes
 */
function batchDeleteUserRole(roleCodes){
	var result = false;
	var userCode = $('#selectUserCode').val();
	var args = {};
	args.userCode = userCode;
	args.roleCodes = roleCodes.join(',');
	$$.ajax({
        url:"/rest/BaseUserRoleBusiness/deleteUserAndRoleData",
        async:false,
        data : args,
        success : function(data){
        	result = true;
        },
        error: function(){
        	result = false;
        }
    });
	return result;
}

$(function () {
    //渲染表格
    var roleColumns = rightsList.roleColumnsInit();
    var roleTable = rightsList.roleGridInit(roleColumns);
    
    var selectRoleColumns = rightsList.selectRoleColumnsInit();
    var selectRoleTable = rightsList.selectRoleGridInit(selectRoleColumns);
    $("#roleTable").bootstrapTable("resetView");
    $("#selectRoleTable").bootstrapTable("resetView");
    //搜索-role
    $('#searchBtnRole').on('click',function(){
    	rightsList.roleDataLoad();
    })
    //回车搜索-role
    $('#queryRole').bind('keypress',function(e){
        if(e.keyCode == 13){
            rightsList.roleDataLoad();
        }
    });
    //重置-role
    $('#searchResetRole').on('click',function(){
    	$('#queryRole').val('');
    	rightsList.roleDataLoad();
    })
    
    //搜索-selectRole
    $('#searchBtnSelectRole').on('click',function(){
    	rightsList.roleSelectDataLoad();
    })
    //回车搜索-selectRole
    $('#querySelectRole').bind('keypress',function(e){
        if(e.keyCode == 13){
            rightsList.roleSelectDataLoad();
        }
    });
    //重置-selectUser
    $('#searchResetSelectRole').on('click',function(){
    	$('#querySelectRole').val('');
    	rightsList.roleSelectDataLoad();
    })
    
    //添加左侧选中用户到右侧
    $('#addToRight').on('click',function(){
    	//已经选择的用户信息
    	var rowDatas = $("#roleTable").bootstrapTable('getSelections');
	    if (rowDatas && rowDatas.length > 0) {
	    	var roleCodes = new Array();
	    	for(var i=0;i<rowDatas.length;i++){
	    		var rowData = rowDatas[i];
	    		roleCodes.push(rowData.roleCode);
	    	}
	    	if(roleCodes.length>0){
	    		//保存数据
	    		if(saveRoleCodes(roleCodes)){
	    			rightsList.roleDataLoad();
	    			rightsList.roleSelectDataLoad();
	    		}
	    	}
	    }
    })
    
    //删除右侧选中用户
    $('#removeRight').on('click',function(){
    	var selectRowDatas = $("#selectRoleTable").bootstrapTable('getSelections');
    	var deleteRoleCode = new Array();
    	if(selectRowDatas && selectRowDatas.length>0){
    		for(var i=0;i<selectRowDatas.length;i++){
	    		var rowData = selectRowDatas[i];
	    		deleteRoleCode.push(rowData.roleCode);
	    	}
    	}
    	if(deleteRoleCode.length>0){
    		if(batchDeleteUserRole(deleteRoleCode)){
	    		rightsList.roleDataLoad();
				rightsList.roleSelectDataLoad();
	    	}else{
	    		
	    	}
    	}
    })
})